package com.dlry.serviceImpl.dwr;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dlry.entity.DlMeetingRoom;
import com.dlry.entity.DlMeetingRoomBooking;
import com.dlry.serviceImpl.MeetingRoomBookingService;
import com.dlry.util.StringUtil;

import net.sf.json.JSONObject;

@Service("meetingRoomBookingService")
public class DWRMeetingRoomBookingService {
	public DWRMeetingRoomBookingService() {
	}

	@Autowired
	private com.dlry.serviceImpl.MeetingRoomService roomService;
	@Autowired
	private MeetingRoomBookingService meetingRoomBookingService;

	public String addBooking(Long roomId, String name, String startstr, String endstr, String userName, String phone) {
		if (roomId == null || name == null || name.equals("") || startstr == null || startstr.equals("")
				|| endstr == null || endstr.equals("") || userName == null || userName.equals("") || phone == null
				|| phone.equals("")) {
			return "信息输入不完整，请补充完整！";
		}
		DlMeetingRoom room = roomService.findById(roomId);
		DlMeetingRoomBooking booking;
		try {
			booking = new DlMeetingRoomBooking();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Timestamp created = new Timestamp(new Date().getTime());
			Timestamp updated = new Timestamp(new Date().getTime());
			Date startDate = format.parse(startstr);
			Date endDate = format.parse(endstr);
			Timestamp start = new Timestamp(startDate.getTime());
			Timestamp end = new Timestamp(endDate.getTime());
			booking.setBookingTime(start);
			booking.setEndTime(end);
			booking.setName(name);
			booking.setUserName(userName);
			booking.setPhone(phone);
			booking.setCreatedTime(created);
			booking.setUpdateTime(updated);
			booking.setRoom(room);
			String validateCode = StringUtil.getRandomString(4) + "-" + StringUtil.getRandomString(4) + "-"
					+ StringUtil.getRandomString(4);
			booking.setVerificationCode(validateCode);
			if (meetingRoomBookingService.save(booking)) {
				return "Y," + booking.getId() + ",预约成功，预约码为：" + booking.getVerificationCode() + "。凭次码修改或删除此次预约。";
			}
			return "预约失败，请检查预约时间是否冲突！";
		} catch (ParseException e) {
			e.printStackTrace();
			return "日期输入错误";
		}
	}

	public JSONObject getBookingInfo(Long bId) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		DlMeetingRoomBooking booking = meetingRoomBookingService.findById(bId);
		if (booking == null) {
			return null;
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", booking.getId());
		jsonObject.put("name", booking.getName());
		jsonObject.put("username", booking.getUserName());
		jsonObject.put("phone", booking.getPhone());
		jsonObject.put("start", format.format(booking.getBookingTime()));
		jsonObject.put("end", format.format(booking.getEndTime()));
		return jsonObject;
	}

	public JSONObject deleteBooking(Long id, String validateCode) {
		JSONObject json = new JSONObject();
		validateCode = validateCode.toUpperCase();
		if (validateCode == null || validateCode.equals("")) {
			json.put("error", "请输入验证码");
		} else {
			DlMeetingRoomBooking booking = meetingRoomBookingService.findById(id);
			if (validateCode.equals(booking.getVerificationCode())) {
				meetingRoomBookingService.deleteByBookingId(id);
				json.put("result", "会议已经取消");
			} else {
				json.put("error", "校验码输入不正确");
			}
		}
		return json;
	}

	public JSONObject updateBooking(Long bId, String name, String startstr, String endstr, String username,
			String phone, String validateCode) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		validateCode = validateCode.toUpperCase();
		JSONObject json = new JSONObject();
		DlMeetingRoomBooking booking = meetingRoomBookingService.findById(bId);
		if (!validateCode.equals(booking.getVerificationCode())) {
			json.put("error", "验证码输入不正确");
		} else {
			try {
				Timestamp start = new Timestamp(format.parse(startstr).getTime());
				Timestamp end = new Timestamp(format.parse(endstr).getTime());
				booking.setBookingTime(start);
				booking.setEndTime(end);
				booking.setName(name);
				booking.setUserName(username);
				booking.setPhone(phone);
				booking.setUpdateTime(new Timestamp(new Date().getTime()));
				boolean reslut = meetingRoomBookingService.update(booking);
				if (reslut) {
					json.put("result", "会议修改成功");
				} else {
					json.put("error", "会议修改失败，请检查时间是够和其他会议冲突");
				}
			} catch (ParseException e) {
				json.put("error", "日期格式输入有错误");
				e.printStackTrace();
			}
		}
		return json;
	}
}
